/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ModifyFurnizor.java
 *
 * Created on Jan 17, 2011, 2:47:00 AM
 */

package proiectpractica;
import java.sql.*;
import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author Claudiu
 */
public class ModifyFurnizor extends javax.swing.JFrame {
    Connection conn;
    int val;
    Statement st;
    int id;
    /** Creates new form ModifyFurnizor */
    public ModifyFurnizor() {
        initComponents();
    }
      public ModifyFurnizor( Connection con , String nume  , String addresa ,int id) {

              initComponents();
         JOptionPane.showMessageDialog(null, "Form pentru Modificare unui  furnizor in baza de date \n Nu lasti nici un camp necompletat!");
              conn= con;
              nume_box.setText(nume);
              this.id = id;
              address_box.setText(addresa);
              try {
            st = conn.createStatement();


              // }
        } catch (SQLException ex) {
            Logger.getLogger(Add_Furnizor.class.getName()).log(Level.SEVERE, null, ex);
                 JOptionPane.showMessageDialog(null, ex.getMessage());
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nume_box = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        address_box = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nume");

        jLabel3.setText("Address");

        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Exit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 421, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(22, 22, 22)
                        .addComponent(nume_box, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(address_box, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 197, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nume_box, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(address_box, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(69, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        val++;
        try {
            if(nume_box.getText().trim().equalsIgnoreCase("")||address_box.getText().trim().equalsIgnoreCase("")) {
                throw new Exception("Date Intrare Incorecte !");
            }
//
//            CallableStatement cs = conn.prepareCall("{call addFurnizorProc(?,?)}");
//            cs.setString(1, nume_box.getText());
//            cs.setString(2, address_box.getText());
             st.executeUpdate("update furnizor set nume = '"+nume_box.getText()+"' , addresa = '"+address_box.getText()+"' where idfurnizor ="+id);
            JOptionPane.showMessageDialog(null, "Cererea dumneavoastra a fost executata cu succes pe server !");
            //st.executeUpdate("call addFurnizorproc ( '"+nume_box.getText()+" ' ,'"+address_box.getText()+" ') ");
            //  st.executeUpdate("insert into scott.client (cod_client , nume , cnp , address) values( '" + val + "' , '" + nume_box.getText() + "' , '" + cnp_box.getText() + "' , '" + address_box.getText()+"' )");
            conn.commit();
            
            // st.exe
            //conn.
            //   ResultSet rs=st.getResultSet();
            // java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            //int colNo = rsmd.getColumnCount();
            
            
            //  Object[] objects = new Object[colNo];
            //  Result.setText(objects[0].toString());
            // tanks to umit ozkan for the bug fix!
            
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
            Logger.getLogger(Add_Furnizor.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ModifyFurnizor().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField address_box;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField nume_box;
    // End of variables declaration//GEN-END:variables

}
